import { getCategoryApi } from '@/api/category'
import { onMounted, ref } from 'vue'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
// 封装分类数据业务相关代码
export const getCategory = () => {
  //根据id获取二级分类数据
  const category = ref({})
  const route = useRoute()
  const getCategory = async (id = route.params.id) => {
    const res = await getCategoryApi(id)
    category.value = res.result
  }
  onMounted(() => {
    getCategory()
  })

  //目标：当路由参数变化时，将分类接口重新调用
  onBeforeRouteUpdate((to) => {
    getCategory(to.params.id)
  })
  return {
    category
  }
}